package com.yunchen.BooksManagementSystem.login.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunchen.BooksManagementSystem.login.entity.Code;
import com.yunchen.BooksManagementSystem.login.entity.Register;
import com.yunchen.BooksManagementSystem.login.service.LoginService;
import com.yunchen.BooksManagementSystem.login.service.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "RegisterServlet",value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    LoginService loginService = new LoginServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        PrintWriter pw = resp.getWriter();

        BufferedReader br = req.getReader();
        // String params = br.readLine();
        StringBuilder builder=new StringBuilder();
        // String params = br.readLine();
        String line=null;
        while ((line=br.readLine())!=null){
            builder.append(line);
        }
        System.out.println(builder);
        Register reg = JSON.parseObject(String.valueOf(builder), Register.class);

        if(loginService.isRegister1(reg.getName(), reg.getGrade(),reg.getDirection())){
            if (!loginService.isRegister2(reg.getUserId())) {
                if (loginService.isRegister3(reg.getInvitation())) {
                    if (!loginService.isRegister4(reg.getName())) {
                        try {
                            loginService.register(reg.getName(), reg.getGrade(), reg.getDirection(), reg.getUserId(), reg.getPassword());
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                        Code code = new Code(002, "注册成功");
                        String json = JSONObject.toJSONString(code);
                        pw.println(json);
                    }else {
                        Code code = new Code(-6, "该学员信息已被注册");
                        String json = JSONObject.toJSONString(code);
                        pw.println(json);
                    }
                }else {
                    Code code = new Code(-5, "错误的邀请码");
                    String json = JSONObject.toJSONString(code);
                    pw.println(json);
                }
            }else {
                Code code = new Code(-4, "该账号已存在");
                String json = JSONObject.toJSONString(code);
                pw.println(json);
            }
        }else {
            Code code = new Code(-3,"该学员不存在或填入信息有误");
            String json = JSONObject.toJSONString(code);
            pw.println(json);
        }
    }
}
